﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using DTD.Repositories;
using DTD.Repositories.Entities;
using DTD.Repositories.Interfaces;
using DTD.Web.Models;

namespace DTD.Web.Controllers
{
    public class HomeController : Controller
    {
        ITaskRepository repository = new TaskRepository();
        ICategoryRepository categoryRepository = new CategoryRepository();
        //
        // GET: /Home/
        public ActionResult Index(int? Id)
        {
            if (Id != null)
                MenuController.SelectedId = Id.Value;


            if (!Id.HasValue)
                Id = Constants.UnorderedCategoryId;
            
            List<Task> result = repository.GetTasksByCategory(Id.Value);
            List<TaskWithCategoryName> categories = result.Select(r => new TaskWithCategoryName {Task = r, CategoryName = categoryRepository.GetCategoryName(r.CategoryId)}).ToList();
            return View(categories);
        }
    }
}
